package com.briup.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.briup.cms.domain.User;
import com.briup.cms.dao.UserDao;
import com.briup.cms.domain.vo.UserVo;
import com.briup.cms.exception.ServiceException;
import com.briup.cms.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.briup.cms.util.JwtUtil;
import com.briup.cms.util.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author briup
 * @since 2023-03-09
 */
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    UserDao userDao;

    @Override
    public void saveOrUpdate(User user) {
        //保存操作
        //1、判断传入的user是否为空
        if(user == null){
            throw new ServiceException(ResultCode.PARAM_IS_BLANK);
        }
        //2、判断username是否为空
        //3、判断数据库当中是否存在该username
        if(user.getUsername()!=null){
            LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper();
            wrapper.eq(User::getUsername,user.getUsername());
            User user1 = userDao.selectOne(wrapper);
            if(user1 != null)
                throw new ServiceException(ResultCode.USER_HAS_EXISTED);
        }
        //4、判断id是否为null（新增）
        if(user.getId()==null){
            if(user.getUsername()==null || user.getPassword()==null)
                throw new ServiceException(ResultCode.PARAM_NOT_COMPLETE);
            //设置一下注册时间
            user.setRegisterTime(LocalDateTime.now());
            //5、新增操作
            userDao.insert(user);
        }else{
            //修改操作
            User user1 = userDao.selectById(user.getId());
            if(user1 == null)
                throw new ServiceException(ResultCode.DATA_NONE);
            //更新操作
            userDao.updateById(user);
        }
        //7、编写对应的Controller以及Swagger注释
    }

    @Override
    public void deleteById(Long id) {
        userDao.deleteById(id);
    }

    @Override
    public User queryById(Long id) {
        User user = userDao.selectById(id);
        if(user == null)
            throw new ServiceException(ResultCode.DATA_NONE);
        return user;
    }

    @Override
    public IPage<UserVo> query(int pageNum, int pageSize, Integer roleId, String username, String status) {
        Page<UserVo> page = new Page<>(pageNum,pageSize);
        IPage<UserVo> list = userDao.getUserVoList(page, roleId, username, status);
//        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper();
//        wrapper.eq(roleId!=null,User::getRoleId,roleId);
//        wrapper.eq(status!=null,User::getStatus,status);
//        wrapper.like(username!=null,User::getUsername,username);
//        userDao.selectPage(page,wrapper);
        return list;
    }

    @Override
    public String login(String username, String password) {
        if(username == null || password == null)
            throw new ServiceException(ResultCode.PARAM_NOT_COMPLETE);
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper();
        wrapper.eq(User::getUsername,username);
        wrapper.eq(User::getPassword,password);
        User user = userDao.selectOne(wrapper);
        if(user == null)
            throw new ServiceException(ResultCode.USER_LOGIN_ERROR);
        //存放要生成token的用户信息
        Map<String,Object> map = new HashMap<>();
        map.put("username",user.getUsername());
        map.put("userId",user.getId());
        String token = JwtUtil.sign(user.getUsername(), map);
        return token;
    }
}
